package util.mysql;
import java.sql.*;
import java.util.Date;
import java.util.LinkedList;

import util.exceltion.MyException;

/**
 * MySQL数据库自定义连接池
 * @author 莫东林
 * @date 2018.11.13
 */
public class MySQLPool {
    private static LinkedList<Connection> pool = new LinkedList<Connection>(); 
    private static int maxCount=1;//最大连接数
    static{//初始化
    	System.out.println("初始化连接池");
        for(int i=0;i<maxCount;i++){
            MySQLHandler handler=new MySQLHandler();
            Connection connection = handler.buildConnection();
            pool.add(connection);
            System.out.println(i+":"+connection);
        }    
    }
    public static Connection getConnecton() throws MyException{
    	
        if(pool.size()==0)//分配完了
        {
            throw new MyException(new Date(),"数据库连接池资源短缺，无连接可分配","数据库连接错误");
        }
        else{
        	return pool.remove(0);
        }
    }
    public static void release(Connection connection){//使用完的归还给池子
        pool.add(connection);
    }
}